package com.example.mydemo;

import androidx.appcompat.app.AppCompatActivity;
import android.accounts.Account;
import android.accounts.OnAccountsUpdateListener;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private int NOTIFICATION_ID = 1;
    private Button button,alertDialogUi,timer,webPage,helpPage,bujuBtn,
            cameraBtn,mainCameraBtn,systemCameraBtn,pref,fragment;
    private Handler mHandler;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.AppTheme);
        setContentView(R.layout.activity_main);
        Log.d("lfy","[MainActivity] onCreate...");

        button = findViewById(R.id.btn);
        button.setOnClickListener(this);
        timer = findViewById(R.id.timer);
        timer.setOnClickListener(this);
        alertDialogUi = findViewById(R.id.alertDialogUi);
        alertDialogUi.setOnClickListener(this);
        webPage = findViewById(R.id.webPage);
        webPage.setOnClickListener(this);

        helpPage = findViewById(R.id.helpPage);
        helpPage.setOnClickListener(this);

        bujuBtn = findViewById(R.id.buju);
        bujuBtn.setOnClickListener(this);

        cameraBtn = findViewById(R.id.camera);
        cameraBtn.setOnClickListener(this);

        mainCameraBtn = findViewById(R.id.mainCamera);
        mainCameraBtn.setOnClickListener(this);

        systemCameraBtn = findViewById(R.id.systemCamera);
        systemCameraBtn.setOnClickListener(this);

        pref = findViewById(R.id.pref);
        pref.setOnClickListener(this);

        fragment = findViewById(R.id.fragment);
        fragment.setOnClickListener(this);

    }

    OnAccountsUpdateListener listener = new OnAccountsUpdateListener() {
        @Override
        public void onAccountsUpdated(Account[] accounts) {
            Log.d("lfy","[MainActivity] onAccountsUpdated...account="+accounts.length);
            for (int i = 0; i < accounts.length; i++) {
                final Account account = accounts[i];
                android.util.Log.d("lfy","[AuthenticatorHelper] onAccountsUpdated...account.name="+account.name+",account.type="+account.type);
            }
        }
    };

        @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn:
                Log.d("lfy","[MainActivity] 点击了发送通知按钮...");
                showNotification();
            case R.id.timer:
                Log.d("lfy","[MainActivity] 点击了开启定时任务按钮...");
                sendTimer();
                Toast.makeText(getApplicationContext(),"111",Toast.LENGTH_LONG).show();
                break;
            case R.id.alertDialogUi:
                Log.d("lfy","[MainActivity] 点击了修改AlertDialog样式按钮...");
                mHandler = new Handler(Looper.getMainLooper());
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
//                        showDialog();
                        showTip();
                    }
                });
                Toast.makeText(getApplicationContext(),"111",Toast.LENGTH_LONG).show();
                break;
            case R.id.webPage:
                Log.d("lfy","[MainActivity] 点击了打开设置中内置网页的界面按钮...");
                intent = new Intent("android.settings.LICENSE3");
                MainActivity.this.startActivity(intent);
                break;
            case R.id.pref:
                Log.d("lfy","[MainActivity] 点击了pref按钮...");
                intent = new Intent("android.intent.action.PREF");
                MainActivity.this.startActivity(intent);
                break;
            case R.id.helpPage:
                Log.d("lfy","[MainActivity] 点击了打开帮助界面按钮...");
                intent = new Intent("android.intent.action.HELP");
                MainActivity.this.startActivity(intent);
                break;
            case R.id.buju:
                Log.d("lfy","[MainActivity] 点击了布局测试按钮...");
                intent = new Intent("android.intent.action.BUJU");
                MainActivity.this.startActivity(intent);
                break;
            case R.id.camera:
                Log.d("lfy","[MainActivity] 点击了相机DEMO按钮...");
                intent = new Intent("android.intent.action.CAMERA");
                MainActivity.this.startActivity(intent);
                break;
            case R.id.mainCamera:
                Log.d("lfy","[MainActivity] 点击了后摄像头按钮...");
                intent = new Intent("android.intent.action.SUBCAMERA");
                MainActivity.this.startActivity(intent);
                break;
            case R.id.systemCamera:
                Log.d("lfy","[MainActivity] 点击了系统摄像头按钮...");
                intent = new Intent("android.intent.action.SYSTEMCAMERA");
                MainActivity.this.startActivity(intent);
                break;
            case R.id.fragment:
                Log.d("lfy","[MainActivity] 点击了系统摄像头按钮...");
                intent = new Intent("android.intent.action.FRAGMENT");
                MainActivity.this.startActivity(intent);
                break;
        }
    }

    private void showNotification(){
        //1、NotificationManager
        NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        /** 2、Builder->Notification
         *  必要属性有三项
         *  小图标，通过 setSmallIcon() 方法设置
         *  标题，通过 setContentTitle() 方法设置
         *  内容，通过 setContentText() 方法设置*/
        Notification.Builder builder = new Notification.Builder(this);

        builder.setContentInfo("Content info")
                .setContentText("Content text")//设置通知内容
                .setContentTitle("Content title")//设置通知标题
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setSmallIcon(R.mipmap.ic_launcher_round)//不能缺少的一个属性
                .setSubText("Subtext")
                .setTicker("滚动消息......")
                .setWhen(System.currentTimeMillis());//设置通知时间，默认为系统发出通知的时间，通常不用设置

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("001","my_channel",NotificationManager.IMPORTANCE_DEFAULT);
            channel.enableLights(true); //是否在桌面icon右上角展示小红点
            channel.setLightColor(Color.GREEN); //小红点颜色
            channel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知
            manager.createNotificationChannel(channel);
            builder.setChannelId("001");
        }

        Notification n = builder.build();
        manager.notify(NOTIFICATION_ID,n);
    }

    private void sendTimer(){
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, SmartRestartService.class);
        intent.setAction("android.service.action.SMARTRESTART");
        Log.d("lfy","[MainActivity] sendTimer...time="+System.currentTimeMillis());
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);
    }

    private void modifyAlertDialogUi(){
        Log.d("lfy", "[MainActivity] modifyAlertDialogUi...");
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(this) //注意这里是this
        .setTitle(R.string.settings_smart_restart)
        .setMessage(R.string.settings_reboot_device_content)
        .setPositiveButton(R.string.okay,new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int whichButton) {
                Log.d("lfy", "[MainActivity] 点击了确定按钮...");
            }
        })
        .setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int whichButton) {
                Log.d("lfy", "[MainActivity] 点击了取消按钮...");
            }
        });
        dialog = builder.create();
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

        Button btnPositive = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
        Button btnNegative = dialog.getButton(AlertDialog.BUTTON_NEGATIVE);

        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) btnPositive.getLayoutParams();
        layoutParams.weight = 1;
        layoutParams.gravity = Gravity.LEFT;
        btnPositive.setLayoutParams(layoutParams);
//        btnNegative.setLayoutParams(layoutParams);

    }

    private void showTip() {
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setPositiveButton(R.string.okay,new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int whichButton) {
                Log.d("lfy", "[AuthenticatorHelper] 点击了确定按钮...");
                dialog.dismiss();
            }
        });
        AlertDialog alertDialog = dialog.create();
        TextView title = new TextView(this);
        title.setText("提示");
        alertDialog.setCustomTitle(title);
        alertDialog.setMessage("更多博客请联系博主，查看xiayiye5博客，地址：https://blog.csdn.net/xiayiye5");
        alertDialog.setCancelable(false);
        alertDialog.show();
    }

//    private void showTip() {
//        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
//        dialog.setPositiveButton(R.string.okay,new DialogInterface.OnClickListener() {
//            public void onClick(DialogInterface dialog,int whichButton) {
//                Log.d("lfy", "[MainActivity] 点击了确定按钮...");
//            }
//        });
//        AlertDialog alertDialog = dialog.create();
//        TextView title = new TextView(this);
//        title.setText("提示");
//        title.setPadding(10, 30, 10, 10);
//        title.setGravity(Gravity.CENTER);
//        title.setTextSize(18);
//        title.setTextColor(Color.BLACK);
//        alertDialog.setCustomTitle(title);
//        alertDialog.setMessage("更多博客请联系博主，查看xiayiye5博客，地址：https://blog.csdn.net/xiayiye5");
//        alertDialog.setCancelable(false);
//        alertDialog.show();
//        Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
//        LinearLayout.LayoutParams cancelBtnPara = (LinearLayout.LayoutParams) button.getLayoutParams();
//        //设置按钮的大小
//        cancelBtnPara.height = LinearLayout.LayoutParams.WRAP_CONTENT;
//        cancelBtnPara.width = LinearLayout.LayoutParams.MATCH_PARENT;
//        cancelBtnPara.gravity = Gravity.LEFT;
//        button.setLayoutParams(cancelBtnPara);
//        button.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary));
//    }

    private void showDialog() {
        final Dialog dialog = new Dialog(this);
        dialog.setCanceledOnTouchOutside(false);
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_layout,null);
        Button okButton = view.findViewById(R.id.okay);
        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                getApplicationContext().startActivity(new Intent().setComponent(getComponentName()));
            }
        });
        dialog.setContentView(view);
        final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = 600;
        params.height = 200;
        dialog.getWindow().setAttributes(params);
        dialog.getWindow().getDecorView().setPadding(50, 30, 50, 30);
        dialog.show();
    }
}
